package org.wzq.test;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import org.json.JSONException;
import org.json.JSONObject;
import org.wzq.base.AppInfo;
import org.wzq.base.DeviceInfo;
import org.wzq.config.AppConfig;
import org.wzq.config.AppUrl;
import org.wzq.mdm.R;
import org.wzq.utils.HttpConnection;
import org.wzq.utils.Logger;

/**
 * project: android
 * date: 14-11-3
 * author: wzq
 * description: TODO
 */
public class ActAuth extends Activity {
    private static Logger logger = Logger.newLogger(true, ActAuth.class);

    private Context mCtx;
    private Button btnAuth;
    private Button btnAppcheck;
    private Button btnDeviceReg;
    private EditText etUserId;
    private EditText etPassword;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.act_test_auth);

        mCtx = this;
        btnAuth = (Button) findViewById(R.id.btn_auth);
        btnAppcheck = (Button) findViewById(R.id.btn_app_check);
        btnDeviceReg = (Button) findViewById(R.id.btn_device_register);

        etUserId = (EditText) findViewById(R.id.et_userId);
        etPassword = (EditText) findViewById(R.id.et_password);

        // 人设绑定
        btnAuth.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        String userId = etUserId.getText().toString();
                        String pwd = etPassword.getText().toString();
                        JSONObject sb = new JSONObject();
                        String body = null;
                        try {
                            sb.put("deviceUuid", DeviceInfo.getDeviceUuid());
                            JSONObject user = new JSONObject();
                            user.put("id", userId);
                            user.put("pwd", pwd);
                            sb.put("user", user);
                            body = sb.toString();
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }

                        String url = AppUrl.getHostIp() + "/device/bind";
                        String result = HttpConnection.post(url, body);
                        logger.v(result == null ? "null" : result);
                    }
                }).start();
            }
        });

        // 软件检测
        btnAppcheck.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // TODO
                String url = AppUrl.getHostIp() + "/appCheck";
                String body = null;
                JSONObject sb = new JSONObject();
                try {
                    sb.put("appKey", AppConfig.APPKEY);
                    sb.put("appInfo", AppInfo.getInfoJson());
                    body = sb.toString();
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                String result = HttpConnection.post(url, body);
                logger.v(result == null ? "null" : result);
            }
        });

        // 设备注册
        btnDeviceReg.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String url = AppUrl.getHostIp() + "/device/reg";
                String body = null;
                JSONObject sb = new JSONObject();
                try {
                    sb.put("deviceInfo", DeviceInfo.getDeviceInfoJson());
                    body = sb.toString();
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                String result = HttpConnection.post(url, body);
                logger.v(result == null ? "null" : result);
            }
        });
    }

    public static String getRegData(String userId, String pwd) {
        JSONObject sb = new JSONObject();
        try {
            sb.put("appKey", AppConfig.APPKEY);
            sb.put("appInfo", AppInfo.getInfoJson());
            sb.put("deviceInfo", DeviceInfo.getDeviceInfoJson());
            JSONObject user = new JSONObject();
            user.put("id", userId);
            user.put("pwd", pwd);
            sb.put("user", user);

            System.out.println(sb.toString());
            System.out.println("appInfo=" + AppInfo.getInfoJson().toString());
            return sb.toString();
        } catch (JSONException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
}